package com.woniu.defecttracking.controller.bugs;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.AccountForProjectDto;
import com.woniu.defecttracking.dto.AccountForProjectsDto;
import com.woniu.defecttracking.dto.FindAccountDto;
import com.woniu.defecttracking.dto.getAccountsByProjectIdDto;
import com.woniu.defecttracking.entity.Account;
import com.woniu.defecttracking.exception.ProjectAccountException;
import com.woniu.defecttracking.param.AddProjectAccountParam;
import com.woniu.defecttracking.param.BasePaperParam;
import com.woniu.defecttracking.param.SelAccountParam;
import com.woniu.defecttracking.service.AccountService;
import com.woniu.defecttracking.service.ProjectAccountService;
import com.woniu.defecttracking.service.impl.ProjectServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 何家欣
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/projectAccount")
public class ProjectAccountController {

    @Autowired
    private ProjectAccountService projectAccountService;
    @Autowired
    private ProjectServiceImpl projectService;
    @Autowired
    private AccountService accountService;

    @GetMapping("/findProjectId/{id}")
    public Result findProjectId(@PathVariable Integer id) {
        System.out.println("findProjectId=====================" + id);
        List<Account> account = projectAccountService.findProjectId(id);
        if (account == null) {
            return Result.getInstance(account).setMessage("查询失败").setCode(500).setData(account);

        }
        return Result.getInstance(account).setMessage("查询成功").setCode(200).setData(account);
    }


    @GetMapping("/getAccountsByProjectId/{projectId}")
    public Result getAccountsByProjectId(@PathVariable("projectId") String projectId) throws ProjectAccountException {
        Result result = new Result();
        List<getAccountsByProjectIdDto> list = projectService.getBaseMapper().selectAccountsByProjectId(projectId);
        if (list != null) {
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(list);
        } else {
            throw new ProjectAccountException("查询项目下成员失败", 1050);
        }

        return result;
    }

    @PostMapping("/getAllUserList")
    public Result getAllUserList(@RequestBody BasePaperParam basePaperParam) throws ProjectAccountException {
        Result result = new Result();
        Page<Account> accountPage = new Page<>(basePaperParam.getPageIndex(), basePaperParam.getPageSize());
        IPage<Account> accountIPage = projectService.getBaseMapper().selectAllUserList(accountPage);
        if (accountIPage != null) {
            result.setData(accountIPage);
            result.setMessage("查询成功");
            result.setCode(200);
        } else {
            throw new ProjectAccountException("查询用户失败", 1051);
        }
        return result;
    }

    @GetMapping("/getAllAccount")
    public Result getAllAccount() {
        List<Account> list = accountService.list();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAccountIsdel() == 1) {
                list.remove(i);
                i--;
            }
        }
        return Result.getInstance(list);
    }

    @PostMapping("/addProjectAccount")
    public Result addAccount(@RequestBody AddProjectAccountParam addProjectAccountParam) {
        projectAccountService.addToProject(addProjectAccountParam);
        return Result.getInstance(null).setMessage("添加成功！");
    }

    @GetMapping("/getProjectAccount/{id}")
    public Result getProjectAccount(@PathVariable Integer id) {
        List<Integer> projectAccounts = projectAccountService.getProjectAccounts(id);
        return Result.getInstance(projectAccounts);
    }

    @PostMapping("/getAByP")
    public Result getProjectAccounts(@RequestBody SelAccountParam selAccountParam) {
        AccountForProjectsDto byProject = projectAccountService.getByProject(selAccountParam);
        return Result.getInstance(byProject);
    }

    @GetMapping("/del/{projectId}/{accountId}")
    public Result getProjectAccount3(@PathVariable Integer projectId,@PathVariable Integer accountId) {
        return Result.getInstance(projectAccountService.del(projectId,accountId));
    }
}
